<template>
  <div class="main">
    <p class="main_text">商户消息列表</p>
    <!-- 搜索 -->
    <div class="g-search" ref="searchBox">
      <!-- 时间选择区域 -->
      <span class="demonstration">创建时间：</span>
      <el-date-picker
        v-model="searchValue.startAt"
        type="date"
        placeholder="开始时间"
        @change="startAt"
        :clearable="false"
        :default-time="defaultTime(0, 0, 0)" />
      <span class="demonstration" style="display: inline-block; font-size: 12px; color: #898989; margin: 20px">至</span>
      <el-date-picker
        v-model="searchValue.endAt"
        type="date"
        placeholder="结束时间"
        @change="endAt"
        :clearable="false"
        :default-time="defaultTime1(23, 59, 59)" />
      <el-radio-group v-model="radio" class="ml-4" @change="radioSelect" style="margin-left: 10px">
        <el-radio label="1" size="large">今天</el-radio>
        <el-radio label="2" size="large">近7天</el-radio>
        <el-radio label="3" size="large">近30天</el-radio>
        <el-radio label="4" size="large">近3个月</el-radio>
      </el-radio-group>

      <!-- 信息区域 -->
      <div class="all_select">
        <!-- 消息标题 -->
        <div class="input_valye">
          <span class="demonstration">店铺ID：</span>
          <input
            type="text"
            class="input"
            placeholder="请输入店铺ID"
            style="width: 200px; color: #606266"
            v-model="searchValue.shopId"
            @keyup.enter="search" />
        </div>
        <div class="input_valye">
          <span class="demonstration">店铺名称：</span>
          <input
            type="text"
            class="input"
            placeholder="请输入店铺名称"
            style="width: 200px; color: #606266"
            v-model="searchValue.shopName"
            @keyup.enter="search" />
        </div>
        <div class="input_valye">
          <span class="demonstration">推送标题：</span>
          <input
            type="text"
            class="input"
            placeholder="请输入推送标题"
            style="width: 200px; color: #606266"
            v-model="searchValue.subject"
            @keyup.enter="search" />
        </div>
        <div class="input_valye">
          <span class="demonstration">推送类型：</span>
          <el-select v-model="searchValue.type" class="m-2" placeholder="请选择推送类型" @change="search">
            <el-option v-for="item in type_options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
      <!-- 底部按钮区域 -->
      <div class="bottom_btn">
        <el-button class="solid" @click="search">查询</el-button>
        <el-button class="hollow" @click="reset">重置</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <div class="cyr-flex">
      <!-- 表格区域 -->
      <el-table
        :data="tableData"
        @selection-change="handleSelectionChange"
        :header-cell-style="{'text-align': 'center'}"
        :show-overflow-tooltip="true"
        v-adaptive
        border>
        <el-table-column prop="sender" label="店铺名称" align="center">
          <template #default="item">
            <el-text tag="span" style="cursor: pointer" @click="lookMes(item.row)">{{ item.row.sender }}</el-text>
          </template>
        </el-table-column>
        <el-table-column prop="subject" label="推送标题" align="center" />
        <el-table-column prop="type" label="推送类型" align="center" width="180">
          <template #default="item">
            <el-text v-if="item.row.type == 0" class="mx-1" type="success">支付通知</el-text>
            <el-text v-if="item.row.type == 1" class="mx-1" type="default">物流通知</el-text>
            <el-text v-if="item.row.type == 2" class="mx-1" type="warning">平台公告</el-text>
            <el-text v-if="item.row.type == 3" class="mx-1" type="primary">优惠促销</el-text>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="接收对象" align="center" />
        <el-table-column prop="sentAt" label="发送时间" align="center" />
        <el-table-column label="操作" width="200" class="operation" :show-overflow-tooltip="false">
          <template #default="item">
            <div class="table_btn">
              <el-button size="small" class="none" @click="lookMes(item.row)">查看</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <smartPager
        v-model:current-page="searchValue.page"
        v-model:page-size="searchValue.rows"
        :total="total"
        @change="pageChange" />
    </div>
    <!-- 添加模板 -->
    <div class="messages" v-if="messagesShow">
      <div class="title1">
        <div class="title_mes">
          <el-icon size="20" color="#fff"> <Edit /> </el-icon>&nbsp;标题
        </div>
        <span class="close" @click="maskBtn1">✖</span>
      </div>
      <div class="content1" style="margin-top: 40px">
        <el-form ref="ruleFormRef" :model="GoodsformDate" label-width="90px" label-position="right" :rules="rules">
          <el-form-item label="消息标题:" prop="type">
            {{ GoodsformDate.title }}
          </el-form-item>
          <el-form-item label="推送类型:" prop="type">
            {{ GoodsformDate.type }}
          </el-form-item>
          <el-form-item label="推送模板:" prop="type">
            {{ GoodsformDate.subject }}
          </el-form-item>
          <el-form-item label="消息内容:" prop="type">
            {{ GoodsformDate.content }}
          </el-form-item>
        </el-form>
        <div class="btn_center">
          <el-button class="hollow" @click="CancelPopUpBox">取消</el-button>
          <el-button class="solid" @click="CancelPopUpBox">确定</el-button>
        </div>
      </div>
    </div>

    <!-- 遮罩层 -->
    <div id="mask1" v-if="mask" @click="maskBtn1"></div>
  </div>
</template>
<script setup lang="ts">
import {ref, onMounted, reactive, onBeforeUnmount} from 'vue';
import {RequestAllShopToUser, PlatformMessageAdd} from '@/api/path/index.js';
import moment from 'moment';
import {ElMessage} from 'element-plus';
import type {FormInstance} from 'element-plus';
import {Warning, Edit, User} from '@element-plus/icons-vue';
const ruleFormRef = ref<FormInstance>();
const defaultTime = (H: any, M: any, S: any) => {
  var now = new Date();
  now.setHours(H);
  now.setMinutes(M);
  now.setSeconds(S);
  return now;
};
const defaultTime1 = (H: any, M: any, S: any) => {
  var now = new Date();
  now.setHours(H);
  now.setMinutes(M);
  now.setSeconds(S);
  return now;
};
const distribution = ref([
  {
    jifen: 200,
    anm: '10',
    anm1: '2',
    anm2: '3',
    anm3: '5',
  },
]);
// 推送消息数据
const GoodsformDate = ref({
  subject: '',
  content: '',
  type: '',
  title: '',
});
// 表单规则
const rules = reactive({
  type: [{required: true, message: '请输入文章标题', trigger: 'blur'}],
  // unit: [{ required: true, message: '请选择商品单位', trigger: 'change' }],
  link: [{required: true, message: '请选择商品分类', trigger: 'change'}],
});
const tableData = ref<any>([]); // 表格列表
const total = ref(0); //表格总数
const mask = ref(false); //控制遮罩层显示
const messagesShow = ref(false); //控制群发显示
const order = ref<any>([]); //订单数组
const radio = ref('');
const shopId = Number(localStorage.getItem('ShopId'));
interface RestaurantItem {
  value: string;
  link: string;
}
const status_list = ref<any>([
  {
    value: 0,
    label: '启用',
  },
  {
    value: 1,
    label: '未启用',
  },
]); //暂时存储状态
// 挂载
onMounted(() => {
  requestAllShopToUser();
  handleResize();
  window.addEventListener('resize', handleResize);
});
const searchBox = ref();
const tablelistHeight = ref();
const handleResize = () => {
  const searchBoxHeight = searchBox.value.clientHeight;
  tablelistHeight.value = 110 + searchBoxHeight + 'px';
  console.log('Search Box height11111111111111111111111111111:', searchBoxHeight);
};
/**
 * 获取模板列表
 */
const requestAllShopToUser = async () => {
  const a = await RequestAllShopToUser(searchValue.value);
  total.value = a.resultObj.allCount;
  tableData.value = a.resultObj.userMessages;
};
/************************************  弹出框关闭  ************************************/
const CancelPopUpBox = () => {
  mask.value = false;
  messagesShow.value = false;
  GoodsformDate.value = {
    subject: '',
    content: '',
    type: '',
    title: '',
  };
};
/************************************  查看信息  ************************************/

const lookMes = (item: any) => {
  console.log('123,', item);
  GoodsformDate.value = {
    subject: item.subject,
    type: item.type,
    content: item.message,
    title: item.title,
  };
  if (item.type == 0) {
    GoodsformDate.value.type = '支付通知';
  } else if (item.type == 0) {
    GoodsformDate.value.type = '物流通知';
  } else if (item.type == 0) {
    GoodsformDate.value.type = '平台公告';
  } else {
    GoodsformDate.value.type = '优惠促销';
  }
  messagesShow.value = true;
  mask.value = true;
};

// 搜索参数
const searchValue = ref({
  subject: '',
  startAt: '',
  endAt: '',
  type: '',
  shopId: '',
  shopName: '',
  page: 1,
  rows: 10,
});
/**
 * 时间范围单选按钮
 */
const radioSelect = () => {
  searchValue.value.startAt = '';
  searchValue.value.endAt = '';
  var now = new Date();
  now.setHours(0);
  now.setMinutes(0);
  now.setSeconds(0);
  var now1 = new Date();
  now1.setHours(23);
  now1.setMinutes(59);
  now1.setSeconds(59);
  if (radio.value === '1') {
    searchValue.value.startAt = moment(now).format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endAt = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '2') {
    searchValue.value.startAt = moment(now).subtract(7, 'days').format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endAt = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '3') {
    searchValue.value.startAt = moment(now).subtract(30, 'days').format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endAt = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  } else {
    searchValue.value.startAt = moment(now).subtract(3, 'months').format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endAt = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  }
  search();
};
// 开始时间
const startAt = () => {
  searchValue.value.startAt.setHours(0);
  searchValue.value.startAt.setMinutes(0);
  searchValue.value.startAt.setSeconds(0);
  searchValue.value.startAt = moment(searchValue.value.startAt).format('yyyy-MM-DD HH:mm:ss');
  radio.value = '0';
  search();
};
// 结束时间
const endAt = () => {
  searchValue.value.endAt.setHours(23);
  searchValue.value.endAt.setMinutes(59);
  searchValue.value.endAt.setSeconds(59);
  searchValue.value.endAt = moment(searchValue.value.endAt).format('yyyy-MM-DD HH:mm:ss');
  radio.value = '0';
  search();
};
/**
 * 搜索
 */
const search = async () => {
  // 判断开始时间不能早于当前时间
  if (moment(searchValue.value.startAt).diff(moment().format('YYYY-MM-DD HH:mm:ss')) >= 0) {
    ElMessage.error('开始时间不能晚于当前日期');
    searchValue.value.startAt = '';
  } else {
    // 判断是否两个时间都填写了
    if (searchValue.value.startAt !== '' && searchValue.value.endAt !== '') {
      // 判断开始时间不能晚于结束时间
      if (moment(searchValue.value.startAt).diff(moment(searchValue.value.endAt)) <= 0) {
        searchValue.value.page = 1;
        requestAllShopToUser();
      } else {
        ElMessage.error('结束时间不能早于开始时间');
        searchValue.value.endAt = '';
      }
    } else {
      // 表示两个时间填写了任意一个
      searchValue.value.page = 1;
      requestAllShopToUser();
    }
  }
};
/**
 * 分页
 */
const pageChange = async (val: number) => {
  requestAllShopToUser();
};
const adminId = JSON.parse(localStorage.getItem('roleInfo') as string).id;
/**
 * 状态选择器
 */
const type_options = [
  {
    value: 0,
    label: '支付通知',
  },
  {
    value: 1,
    label: '物流通知',
  },
  {
    value: 2,
    label: '平台公告',
  },
  {
    value: 3,
    label: '优惠促销',
  },
];
/**
 * 点击重置
 */
const reset = () => {
  searchValue.value = {
    subject: '',
    startAt: '',
    endAt: '',
    type: '',
    page: 1,
    rows: 15,
    shopId: '',
    shopName: '',
  };
  requestAllShopToUser();
  radio.value = '0';
  status_list.value = '';
};
/**
 * 表格复选框选中事件
 */
const handleSelectionChange = (selection: any) => {
  order.value = [];
  for (let x = 0; x < tableData.value.length; x++) {
    tableData.value[x].selection = false;
  }
  for (let i = 0; i < selection.length; i++) {
    selection[i].selection = true;
    order.value.push(selection[i]);
  }
};

/**
 * 遮罩层数据
 */
const maskBtn1 = () => {
  mask.value = false;
  messagesShow.value = false;
  status_list.value = '';
};
</script>
<style lang="scss" scoped>
@import '@/assets/css/plateform/Article/Content.css';
@import '@/assets/css/plateform/Shop/GoodsManage.css';

.main {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.table {
  //height: calc(100% - v-bind(tablelistHeight)) !important;
}

::v-deep .el-table--fit {
  //height: calc(100% - 70px) !important;
}

.table {
  padding: 10px !important;
}

.table :deep(.el-table__cell) {
  text-align: center;
}

.nav1 {
  display: flex;
  height: 30px;
  align-items: center;
  margin-top: 20px;
  border-bottom: 0;

  .nav_li {
    width: 50%;
    height: 30px;
    line-height: 30px;
    text-align: center;
    font-size: 14px;
  }

  .current {
    border-bottom: 2px solid #5697f9;
  }
}

.table {
  overflow: hidden;
  padding: 0 10px;

  .table_function {
    margin-top: 20px;
  }
}

.el-form-item :deep(.el-input-group__prepend) {
  width: 10px;
  background-color: #116cf8;
  color: #fff;
}

.el-form-item :deep(.el-upload--picture-card) {
  height: 100px;
  width: 100px;
  opacity: 0;
}

.btn {
  width: 150px;
  position: absolute;
  right: 20px;
  bottom: 40px;
}

// 推送消息
.plateform,
.messages {
  width: 600px;
  height: 500px;
  position: absolute;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-left: -300px;
  margin-top: -250px;
  z-index: 99;
  background: #fff;
}

.messages {
  height: 400px;
  margin-top: -200px;
}

:deep(.el-input__wrapper) {
  width: 200px;
}

:deep(.el-drawer.rtl) {
  background: #fff;
  width: 750px !important;
}

:deep(.el-overlay) {
  z-index: 40 !important;
  padding: static;
}

:deep(.is-process) {
  color: #409eff;
}

:deep(.is-process .el-step__icon.is-text) {
  border: 2px solid #409eff;
  background: #409eff;
  color: #fff;
}

:deep(.is-finish .el-step__line) {
  background: #409eff;
}

.el-table .warning-row {
  background: #44a2ff !important;
}

:deep(.el-select) {
  width: 100%;
}
</style>
